using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data;
using InfoExpress.HIV.Domains;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.WinApp.Utils;
using InfoExpress.HIV.WinApp.Business;

namespace InfoExpress.HIV.WinApp.Popups
{
  public partial class FrmChonBenhNhan : XtraForm
  {
    #region Members and Properties

    private Type _invokeForm;

    /// <summary>
    /// Ma benh nhan.
    /// </summary>
    public string MaBenhNhan
    {
      get
      {
        return txtMaThe.Text;
      }
    }
    /// <summary>
    /// Ngay dang ky.
    /// </summary>
    public DateTime NgayDangKy
    {
      get
      {
        return deNgayDangKy.DateTime;
      }
    }
    /// <summary>
    /// Benh nhan Id.
    /// </summary>
    public int BenhNhanId { get; set; }

    #endregion

    #region Constructors

    public FrmChonBenhNhan()
    {
      InitializeComponent();
      deNgayDangKy.DateTime = DateTime.Today;
    }

    public FrmChonBenhNhan(Type invokeForm) 
      : this()
    {
      _invokeForm = invokeForm;
    }

    #endregion

    #region Event Handlers

    #region Command Handlers

    private void btnSelect_Click(object sender, EventArgs e)
    {
      if (dxValidationProvider.Validate())
      {
        try
        {
          if (txtMaThe.Text.Substring(3, 1) != "0")
          {
            int total = 0;
            TList<BenhNhanDangKy> benhNhanList =
              GlobalCache.BenhNhanDangKyService.GetPaged(
              string.Format("MaBenhNhan='{0}' AND DeleteFlag=0", txtMaThe.Text),
              string.Empty, 0, 1, out total);

            if (total > 0)
            {
              if (benhNhanList[0].TrangThaiId == (int)BenhNhanUtils.TrangThai.Duyet)
              {
                if (CheckSpecificCondition(_invokeForm, benhNhanList[0].BenhNhanId))
                {
                  using (Form frm = Activator.CreateInstance(_invokeForm) as Form)
                  {
                    BenhNhanId = benhNhanList[0].BenhNhanId;
                    frm.Owner = this;
                    DialogResult = frm.ShowDialog();
                  }

                  Close();
                }
              }
              else
                UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_004);
            }
            else
              UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_001);
          }
          else
            UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_002);
        }
        catch (Exception ex)
        {
          Logger.LogCritical("FrmChonBenhNhan::btnSelect_Click", ex, "General", null);
          UIUtils.ShowCannotLoadDataMessage();
        }
      }
    }

    #endregion

    #endregion

    #region Helper Methods

    private bool CheckSpecificCondition(Type invokeForm, int benhNhanId)
    {
      bool isValid = true;

      if (invokeForm == typeof(FrmDkDieuTriArv))
      {
        #region FrmDkDieuTriArv
        DataSet dtsResult = GlobalCache.DieuTriService.KiemTraTonTai(MaBenhNhan, true, true, true, null);
        DataTable dtbResult = dtsResult.Tables[0];

        if (dtbResult.Rows.Count == 0)
        {
          int total = 0;
          TList<DieuTri> dieuTriList =
            GlobalCache.DieuTriService.GetPaged(
              string.Format("BenhNhanId='{0}' AND DeleteFlag=0", benhNhanId),
              string.Empty, 0, 1, out total);

          if (total > 0)
          {
            if (dieuTriList[0].MaChamDutDieuTri == null)
            {
              ChuyenOpcBuffer chuyenOpcBuffer =
              GlobalCache.ChuyenOpcBufferService.GetByBenhNhanId(
                BenhNhanUtils.LayBenhNhanId(txtMaThe.Text));

              if (chuyenOpcBuffer == null)
              {
                if (dieuTriList[0].DieuTriArv)
                {
                  isValid = false;
                  UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_006);
                }
              }
              else
              {
                isValid = false;
                UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_008);
              }
            }
            else
            {
              isValid = false;
              UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_005);
            }
          }
          else
          {
            isValid = false;
            UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_007);
          }
        }
        else
        {
          isValid = false;
          UIUtils.ShowWarningMessage(String.Format(Constants.MsgWrnChonBnDieuTri_003,
            dtbResult.Rows[0]["MaDonVi"].ToString(),
            dtbResult.Rows[0]["TenDonVi"]));
        }
        #endregion
      }
      else if (invokeForm == typeof(FrmChamDutDieuTri))
      {
        #region FrmChamDutDieuTri
        int total = 0;
        TList<DieuTri> dieuTriList =
          GlobalCache.DieuTriService.GetPaged(
            string.Format("BenhNhanId='{0}' AND DeleteFlag=0", benhNhanId),
            "Id DESC", 0, 1, out total);

        if (total > 0)
        {
          if (dieuTriList[0].MaChamDutDieuTri == null)
          {
            
          }
          else
          {
            isValid = false;
            UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_005);
          }
        }
        else
        {
          isValid = false;
          UIUtils.ShowWarningMessage(Constants.MsgWrnChonBnDieuTri_007);
        }
        #endregion
      }

      return isValid;
    }

    #endregion
  }
}